//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using LitwareHR.Recruiting.Contract;
using LitwareHR.Recruiting.Workflows.Activities;

namespace LitwareHR.Recruiting.Workflows
{
    public sealed partial class EvaluationProcess : SequentialWorkflowActivity
    {
        private string _applicantUsername;
        private string _nextStep;
        private bool _positive;
        private Guid _recruitingWorkflowId;
        private Guid _tenantId;
        private string _waitingFor;
        private Guid _positionId;       
        private Guid _resumeId;

        public EvaluationProcess()
        {
            InitializeComponent();
        }

        public string ApplicantUsername
        {
            get { return _applicantUsername; }
            set { _applicantUsername = value; }
        }

        public string NextStep
        {
            get { return _nextStep; }
            set { _nextStep = value; }
        }

        public bool Positive
        {
            get { return _positive; }
            set { _positive = value; }
        }

        public Guid RecruitingWorkflowId
        {
            get { return _recruitingWorkflowId; }
            set { _recruitingWorkflowId = value; }
        }

        public Guid TenantId
        {
            get { return _tenantId; }
            set { _tenantId = value; }
        }

        public string WaitingFor
        {
            get { return _waitingFor; }
            set { _waitingFor = value; }
        }

        public Guid PositionId
        {
            get { return _positionId; }
            set { _positionId = value; }
        }

        public Guid ResumeId
        {
            get { return _resumeId; }
            set { _resumeId = value; }
        }

        void activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)
        {
            HumanInteractionActivity activity = ((HumanInteractionActivity)e.Activity);
            Positive = activity.Positive;
        }

        void activity_Executing(object sender, ActivityExecutionStatusChangedEventArgs e)
        {
            HumanInteractionActivity activity = ((HumanInteractionActivity)e.Activity);
            activity.Positive = Positive;
            GetNextStep(activity);
            GetWaitingFor(activity);
        }

        private void GetNextStep(HumanInteractionActivity interviewActivity)
        {
            string interviewActivityType = interviewActivity.GetType().Name;
            switch (interviewActivityType)
            {
                case Constants.InterviewActivityTypeName:
                    _nextStep = Constants.InterviewStep;
                    break;
                case Constants.NegotiationActivityTypeName:
                    _nextStep = Constants.NegotiationStep;
                    break;
                case Constants.ResultActivityTypeName:
                    if (Positive)
                    {
                        _nextStep = Constants.AcceptanceStep;
                    }
                    else
                    {
                        _nextStep = Constants.RejectionStep;
                    }
                    break;
            }
        }

        private void GetWaitingFor(HumanInteractionActivity interviewActivity)
        {
            _waitingFor = interviewActivity.WorkflowRoles[0].Name;
        }

        private void onCompleted(object sender, EventArgs e)
        {
            if (Positive)
            {
                _nextStep = Constants.AcceptedStep;
            }
            else
            {
                _nextStep = Constants.NotAcceptedStep;
            }
        }


        private void onInitialized(object sender, EventArgs e)
        {
            foreach (Activity activity in Activities)
            {
                activity.Executing += new EventHandler<ActivityExecutionStatusChangedEventArgs>(activity_Executing);
                activity.Closed += new EventHandler<ActivityExecutionStatusChangedEventArgs>(activity_Closed);
            }

            Positive = true;
        }
    }
}
